package com.gpnu7_412.yanzhi.interceptors;

import com.gpnu7_412.yanzhi.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

/**
 * 管理员权限拦截器
 * @author ZZH
 */
@Component
public class ManagerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        if("OPTIONS".equals(request.getMethod().toUpperCase())) {
            return true;
        }

        Map<String, Object> map = ThreadLocalUtil.get();
        String permission = (String) map.get("permission");
        if ("管理员".equals(permission)) {
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
